﻿Imports EVOLTA.Promocion.Bl.Be
Imports EVOLTA.Promocion.Bl.Bc

Partial Class mantenedores_tpoCambio
    Inherits System.Web.UI.Page

    Private cambios As List(Of EtpoCambio)
    Dim expression As String = Nothing
    Dim direction As SortDirection = SortDirection.Ascending

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.CacheControl = "no-cache"
        Response.AddHeader("Pragma", "no-cache")
        Response.Expires = -1
        '***********************************************************'
        btnAgregar.Attributes.Add("onclick", "MuestraPanel(); return false;")
        If Not IsPostBack Then
            cargaraño()
            cargarmes()
            cargartpoCambio()
        End If
        Dim scriptText As String = ""
        scriptText &= "function mensaje(){"
        scriptText &= "    alert('se graba correctamente.');"
        scriptText &= "}"
        ClientScript.RegisterClientScriptBlock(Me.GetType(), "mensaje()", scriptText, True)

    End Sub
    Protected Sub btnBuscar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBuscar.Click
        gvwLista.DataSource = Buscar(selMes.Value, selAño.Value)
        gvwLista.DataBind()
    End Sub
    Private Function Buscar(ByVal mes As Int32, ByVal año As Int32) As IEnumerable(Of EtpoCambio)
        Dim LtpoCambio As System.Collections.Generic.IEnumerable(Of EtpoCambio) = Nothing
        cambios = Session("lista")
        If Not cambios Is Nothing Then
            If mes > 0 AndAlso año = 0 Then
                LtpoCambio = From v In cambios _
                            Where CType(v.fecha, Date).Month = mes _
                            Select v
            ElseIf mes = 0 AndAlso año > 0 Then
                LtpoCambio = From v In cambios _
                            Where CType(v.fecha, Date).Year = año _
                            Select v
            ElseIf mes > 0 AndAlso año > 0 Then
                LtpoCambio = From v In cambios _
                            Where CType(v.fecha, Date).Month = mes AndAlso CType(v.fecha, Date).Year = año _
                            Select v
            Else

                LtpoCambio = From v In cambios _
                             Select v
                Return LtpoCambio
            End If
        End If
        Return LtpoCambio
    End Function
    Protected Sub lnkBtn_Command(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs) _
    Handles lnkBtnFecha.Command, lnkBtnCambioCompra.Command, lnkBtnCambioVenta.Command
        Dim cambio As IEnumerable(Of EtpoCambio) = Buscar(selMes.Value, selAño.Value)

        Select Case e.CommandName
            Case "fecha" : expression = e.CommandArgument
                Dim List = From d In cambio _
                           Order By d.fecha _
                           Select d
                gvwLista.DataSource = List
                gvwLista.DataBind()
            Case "valorCompra" : expression = e.CommandArgument
                Dim List = From d In cambio _
                           Order By d.valorCompra _
                           Select d
                gvwLista.DataSource = List
                gvwLista.DataBind()
            Case "valorVenta" : expression = e.CommandArgument
                Dim List = From d In cambio _
                           Order By d.valorVenta _
                           Select d
                gvwLista.DataSource = List
                gvwLista.DataBind()
        End Select
    End Sub
    Protected Sub gvwLista_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvwLista.PageIndexChanging
        'Me.gvwLista.PageIndex = e.NewPageIndex
        'Me.gvwLista.DataBind()
    End Sub
    Private Sub cargartpoCambio()
        cambios = RtpoCambio.SeleccionarTodo(Me.Master.descEmpresa, False)
        Session("lista") = cambios
        If Not cambios Is Nothing Then
            gvwLista.DataSource = cambios
            gvwLista.DataBind()
        End If
    End Sub
    Private Sub cargarmes()
        Dim EMes As New Ecommons
        selMes.DataSource = EMes.mes
        selMes.DataTextField = "descripcion"
        selMes.DataValueField = "codigo"
        selMes.DataBind()
    End Sub
    Private Sub cargaraño()
        Dim EAño As New Ecommons
        selAño.DataSource = EAño.año
        selAño.DataTextField = "descripcion"
        selAño.DataValueField = "codigo"
        selAño.DataBind()
    End Sub
    Protected Sub gvwLista_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) _
    Handles gvwLista.RowCommand
        Select Case e.CommandName
            Case "Edit"
                'Dim id As String = e.CommandArgument
            Case "Delete"
                Dim pTpoCambio As EtpoCambio = New EtpoCambio
                With pTpoCambio
                    .fecha = e.CommandArgument
                    .idUsuario = Me.Master.idUsuario
                End With
                RtpoCambio.Eliminar(Me.Master.descEmpresa, pTpoCambio)
                cargartpoCambio()
        End Select
    End Sub
    Protected Sub gvwLista_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) _
    Handles gvwLista.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.Attributes.Add("OnMouseOver", "Resaltar_On(this);")
            e.Row.Attributes.Add("OnMouseOut", "Resaltar_Off(this);")
            e.Row.Attributes("OnClick") = Page.ClientScript.GetPostBackClientHyperlink(Me.gvwLista, "Select$" + e.Row.RowIndex.ToString)
            e.Row.Style.Add("cursor", "hand")
        End If
    End Sub
    Protected Sub gvwLista_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvwLista.RowDeleting
        'gvwLista.Visible = countList > 0
    End Sub
    Protected Sub gvwLista_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvwLista.RowEditing
        Dim id As String = CType(CType(gvwLista.Rows(e.NewEditIndex).FindControl("lnkFecha"), LinkButton).Text, String)
        cambios = Session("lista")
        Dim LtpoCambio = From n In cambios _
                            Where n.fecha.ToString = id _
                            Select n
        lblTitulo.Text = "Tipo de Cambio [Modificar]"
        lblTitulo.ToolTip = "Modificar"

        txtfecha.Text = LtpoCambio.ElementAt(0).fecha
        txtValorCompra.Text = LtpoCambio.ElementAt(0).valorCompra.ToString("#.000")
        txtValorVenta.Text = LtpoCambio.ElementAt(0).valorVenta.ToString("#.000")

        mpDetalle.Show()
    End Sub
    Protected Sub btnGuardar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGuardar.Click
        If txtValorCompra.Text.Trim.Length > 0 AndAlso txtValorVenta.Text.Trim.Length Then
            Dim jsMensaje As String = Nothing
            Dim pTpoCambio As EtpoCambio = Nothing
            If lblTitulo.ToolTip = "Registrar" Then
                pTpoCambio = New EtpoCambio
                With pTpoCambio
                    .fecha = txtfecha.Text
                    .valorCompra = Convert.ToDecimal(txtValorCompra.Text)
                    .valorVenta = Convert.ToDecimal(txtValorVenta.Text)
                    .idUsuario = Me.Master.idUsuario
                End With
                Dim resultado As Boolean = RtpoCambio.Añadir(Me.Master.descEmpresa, pTpoCambio)
                If resultado Then
                    jsMensaje = "<script>javascript:alert(' Se registro correctamente.');</script>)"
                    Controls.Add(New LiteralControl(jsMensaje))
                    cargartpoCambio()
                Else
                    txtMensajes.Value = "No se pudo registrar."
                End If
            ElseIf lblTitulo.ToolTip = "Modificar" Then
                pTpoCambio = New EtpoCambio
                With pTpoCambio
                    .fecha = txtfecha.Text
                    .valorCompra = Convert.ToDecimal(txtValorCompra.Text)
                    .valorVenta = Convert.ToDecimal(txtValorVenta.Text)
                    .idUsuario = Me.Master.idUsuario
                End With
                RtpoCambio.Editar(Me.Master.descEmpresa, pTpoCambio)
                jsMensaje = "<script>javascript:alert(' Se modifico correctamente.');</script>)"
                Controls.Add(New LiteralControl(jsMensaje))
                cargartpoCambio()
            End If
            lblTitulo.ToolTip = "Registrar"
        End If
    End Sub
    Protected Sub btnCancelar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        lblTitulo.ToolTip = "Registrar"
        mpDetalle.CancelControlID = "btnCancelar"
    End Sub
    Protected Sub btnExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExcel.Click
        cambios = Session("lista")
        If Not cambios Is Nothing Then
            Dim fileName As String = "Consulta{0}.xls"
            fileName = String.Format(fileName, DateTime.Now.ToShortDateString())
            Dim Response As HttpResponse = HttpContext.Current.Response
            Response.ContentType = "application/vnd.ms-excel"
            Response.Charset = "UTF-8"
            Response.ContentEncoding = System.Text.Encoding.Default
            Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName)

            Response.Write("Fecha")
            Response.Write(vbTab)
            Response.Write("Valor Tipo Compra")
            Response.Write(vbTab)
            Response.Write("Valor Tipo Venta")
            Response.Write(vbTab)
            Response.Write(vbCrLf)

            Dim value As String
            For Each cambio In cambios
                value = cambio.fecha
                Response.Write(value)
                Response.Write(vbTab)

                value = cambio.valorCompra
                Response.Write(value)
                Response.Write(vbTab)

                value = cambio.valorVenta
                Response.Write(value)
                Response.Write(vbCrLf)
            Next
            Response.End()
        Else
            Dim jsMensaje As String = Nothing
            jsMensaje = "<script>javascript:alert(' La consulta no pudo ser procesada.  Por favor inténtelo de nuevo.');</script>)"
            Controls.Add(New LiteralControl(jsMensaje))
        End If
    End Sub

End Class
